package cn.nfwx.tv.adapter;

import android.support.annotation.LayoutRes;
import android.util.SparseArray;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import cn.nfwx.tv.R;
import cn.nfwx.tv.db.AppDatabase;
import cn.nfwx.tv.db.DownProgramDao;
import cn.nfwx.tv.db.DownProgramModel;
import cn.nfwx.tv.helper.AccountHelper;
import cn.nfwx.tv.model.TVProgramModel;
import cn.nfwx.tv.model.TilteModel;
import cn.nfwx.tv.model.TvProgram;
import cn.nfwx.tv.util.TimeUtil;

/**
 * Created by dell on 2017/11/13.
 */

public class SearchAdapter extends BaseQuickAdapter<Object, BaseViewHolder> {

    private final String TAG = SearchAdapter.class.getSimpleName();

    public static final int ITEM_TITLE = 0;
    public static final int ITEM_PART1 = 1;
    public static final int ITEM_PART2 = 2;
    public static final int ITEM_PART3 = 3;
    private SparseArray<Integer> layouts;

    public SearchAdapter() {
        super(null);
        addItemType(ITEM_TITLE, R.layout.item_search_title_layout);
        addItemType(ITEM_PART1, R.layout.item_search_part1_layout);
        addItemType(ITEM_PART2, R.layout.item_search_part2_layout);
        addItemType(ITEM_PART3, R.layout.item_search_loadmore_layout);
    }

    protected void addItemType(int type, @LayoutRes int layoutResId) {
        // layouts
        if (layouts == null) {
            layouts = new SparseArray<>();
        }
        layouts.put(type, layoutResId);
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        return createBaseViewHolder(parent, getLayoutId(viewType));
    }

    @Override
    protected void convert(BaseViewHolder helper, Object item) {
        if (item instanceof String) { // 标题
            helper.setText(R.id.item_sreach_title, (String) item);
        } else if (item instanceof TVProgramModel) { // 节目
            helper.setText(R.id.btn_appiontment_downlaod, R.string.booking);
            TVProgramModel data = (TVProgramModel) item;
            if (data.getTime() > 0) {
                helper.setText(R.id.tv_vod_name, data.getTitle() + "-" + TimeUtil.getFormat2Time(data.getTime()));
            } else {
                helper.setText(R.id.tv_vod_name, data.getTitle());
            }
            helper.getView(R.id.tv_vod_name).setSelected(true);
        } else if (item instanceof TilteModel) { // 尾部
            TilteModel data = (TilteModel) item;
            helper.setText(R.id.tv_load_more, data.textId);
        } else if (item instanceof TvProgram) {
            TvProgram data = (TvProgram) item;
            helper.getView(R.id.tv_vod_name).setSelected(true);
            helper.setVisible(R.id.tv_vod_note, false);
            helper.setText(R.id.btn_appiontment_downlaod, R.string.download);
            helper.setVisible(R.id.btn_appiontment_downlaod, true);
            if (data.getType() == 0) {
                helper.setText(R.id.tv_vod_name, data.getProgram_name() + "-" + data.getTime());
                helper.setVisible(R.id.tv_vod_note, false);
                helper.setText(R.id.btn_appiontment_downlaod, R.string.download);
                if (AccountHelper.getInstance().isLogin(helper.getConvertView().getContext())) {
                    DownProgramDao downProgramDao = AppDatabase.getInstance(mContext).programDao();
                    List<DownProgramModel> d = downProgramDao.queryAllByUrl(data.getUrl());
                    if (!d.isEmpty()) {
                        DownProgramModel downProgramModel = d.get(0);
                        if (downProgramModel.getStats() == DownProgramModel.STATS_DOWNLOADING
                                || downProgramModel.getStats() == DownProgramModel.STATS_PAUSE
                                || downProgramModel.getStats() == DownProgramModel.STATS_DEFAULT
                                || downProgramModel.getStats() == DownProgramModel.STATS_WAIT) {
                            helper.setText(R.id.tv_vod_note, R.string.downloading);
                            helper.setVisible(R.id.tv_vod_note, true);
                            helper.setVisible(R.id.btn_appiontment_downlaod, false);
                        } else if (downProgramModel.getStats() == DownProgramModel.STATS_SUCCESS) {
                            helper.setText(R.id.tv_vod_note, R.string.downloaded);
                            helper.setVisible(R.id.tv_vod_note, true);
                            helper.setVisible(R.id.btn_appiontment_downlaod, false);
                        } else {
                            helper.setText(R.id.btn_appiontment_downlaod, R.string.download);
                            helper.setVisible(R.id.btn_appiontment_downlaod, true);
                        }
                    } else {
                        helper.setText(R.id.btn_appiontment_downlaod, R.string.download);
                        helper.setVisible(R.id.btn_appiontment_downlaod, true);
                    }
                }
            } else if (data.getType() == 2) {
                boolean isEmpty = AppDatabase.getInstance(mContext).appointDao().queryAllById(data.getProgram_id()).isEmpty();
                if (isEmpty || !AccountHelper.getInstance().isLogin(mContext)) {
                    helper.setText(R.id.btn_appiontment_downlaod, R.string.booking);
                    helper.setText(R.id.tv_vod_name, data.getProgram_name() + "-" + data.getTime());
                    helper.setVisible(R.id.tv_vod_note, false);
                } else {
                    helper.setVisible(R.id.tv_vod_note, true);
                    helper.setText(R.id.tv_vod_note, R.string.appiontmented);
                    helper.setText(R.id.tv_vod_name, data.getProgram_name() + "-" + data.getTime());
                    helper.setVisible(R.id.btn_appiontment_downlaod, false);
                }
            }
            helper.getView(R.id.tv_vod_name).setSelected(true);
        }
        helper.addOnClickListener(R.id.btn_appiontment_downlaod);
        helper.addOnClickListener(R.id.subscription_btn);
        helper.addOnClickListener(R.id.subscription_cancle_btn);
    }


    private int getLayoutId(int viewType) {
        return layouts.get(viewType);
    }

    @Override
    protected int getDefItemViewType(int position) {
        Object object = getData().get(position);
        if (object instanceof String) {
            return ITEM_TITLE;
        } else if (object instanceof TVProgramModel) {
            return ITEM_PART2;
        } else if (object instanceof TvProgram) {
            return ITEM_PART2;
        }
        return ITEM_PART3;
    }
}
